package com.cloudsigns.factorymanager2.engineering.model;

import android.net.Uri;
import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.UserInfoBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/3/28.
 */

public class EngineeringUsersListModel implements IEngineeringUsersListModel {

    @Override
    public void getUserList(final CommonEnum.ROLECATEGORY rolecategory, final OnEngineeringUserListListener engineeringUserListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String rolecategoryStr = "";
                if (null != rolecategory) {
                    if (rolecategory == CommonEnum.ROLECATEGORY.SUPER) {
                        rolecategoryStr = "SUPER";
                    } else if (rolecategory == CommonEnum.ROLECATEGORY.MANAGER) {
                        rolecategoryStr = "MANAGER";
                    } else if (rolecategory == CommonEnum.ROLECATEGORY.TEAM) {
                        rolecategoryStr = "TEAM";
                    }
                }

                try {

                    String uri = ProjectUtil.ENGINEERING_USER_LIST_URL + "?roleCategory=" + rolecategoryStr;
                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(uri);

                    HttpResponse response;

                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != engineeringUserListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("accountItemList"));
                            List<UserInfoBean> userInfoBeanList = new ArrayList<UserInfoBean>();
                            if (jsonArr != null) {

                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        UserInfoBean userInfoBean = new UserInfoBean();
                                        userInfoBean.setId(jsonObj.getString("id"));
                                        userInfoBean.setName(jsonObj.getString("name"));

                                        userInfoBeanList.add(userInfoBean);

                                    }
                                }

                                engineeringUserListListener.successed(userInfoBeanList);

                            }


                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            engineeringUserListListener.failed(error_str);

                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnEngineeringUserListListener {
        void successed(List<UserInfoBean> userInfoBeanList);

        void failed(String desc);
    }


}
